#include "threadPool.h"

char testArr[4096];

void split(char*src,char* separator,char** dest,int* num)
{
    char* pNext;
    int count = 0;

    char* strtok(char*str,const char* delim);
    pNext = strtok(src,separator);
     while(pNext != NULL)
     {
         *dest++ = pNext;
         ++count;
         pNext = strtok(NULL,separator);
     }
    *num = count;
}


int myCd(char* real_path,char* command)
{
    if(strcmp(command,".") == 0)
    {
        while(*real_path)
        {
            printf("%c",*real_path);
            real_path++;
        }
        printf("\n");
    }
    else if(strcmp(command,"..") == 0)
    {   char temp[4096] = {0};
        strcpy(testArr,real_path);
        char output1[1024] = {0};
        char output2[1024] = {0};
        //剪切字符串i,除了最后一个都打印
        char* splitArr[1024] = {0};
        int num = 0;
        split(testArr,"/",splitArr,&num);
        if(num <= VIRTUAL_START_CD)
        {
            printf("已经位于根目录，不能回退！\n");
        }
        else
        {            
            for(int i = 0; i < num - 1; i++)
            {
                sprintf(output1,"%s%s","/",splitArr[i]);
                strcat(output2,output1);
            }
            strcpy(real_path,output2);
            strcpy(testArr,temp);
            //testArr为中转
            printf("%s\n",real_path);
        }
    }
    else if(strcmp(command,"/") == 0)
    {
        char temp[4096] = {0};
        strcpy(testArr,real_path);
        char output1[1024] = {0};
        char output2[1024] = {0};
        //只打印到用户名的字符串
        char* splitArr[1024] = {0};
        int num = 0;
        split(testArr,"/",splitArr,&num);
        for(int i = 0; i < VIRTUAL_START_CD; i++)
        {
            sprintf(output1,"%s%s","/",splitArr[i]);
            strcat(output2,output1);
        }
        strcpy(real_path,output2);
        strcpy(testArr,temp);
        //测试
        printf("%s\n",real_path);
    }
    else
    {
        char temp[4096] = {0};
        sprintf(temp,"%s%s%s",real_path,"/",command);
        DIR* pdir = opendir(temp);
        if(pdir != NULL)
        {
            closedir(pdir);
            strcpy(real_path,temp);
            //sprintf(real_path,"%s%s%s",real_path,"/",command);
            //测试
            printf("%s\n",real_path);
        }
        else
        {
            printf("该目录不存在！\n");
        }
    }

    return 0;
}
